﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApp1
{
    public class Solution10
    {
        public int StrStr(string haystack, string needle)
        {
            char[] HayStack = haystack.ToCharArray();
            char[] Needle = needle.ToCharArray();
            int flag = 0;
            int i = 0;
            if (needle.Length==0)
            {
                return 0;
            }
           
                int j = 0;
                while (j < needle.Length)
                {
                    if (i >=haystack.Length)
                    {
                        return -1;
                    }
                    if (HayStack[i] == Needle[j])
                    {
                        i++;
                        j++;
                        flag++;
                    }
                    else
                    {
                        j = 0;
                        i = i + 1 - flag ;
                        flag = 0;
                    }
                }
            
            if (flag==needle.Length)
            {
                return i-needle.Length;
            }
            return -1;
        }
    }
}
